﻿
Shader "Dan/UI/UI_LoadingPolygon" {
    Properties {
        _ColorOutside ("ColorOutside", Color) = (0.07843138,0.3921569,0.7843137,1)
        _ColorInside ("ColorInside", Color) = (1,0,0,1)
        _Emission ("Emission", Float ) = 1
        _PolygonSides("PolygonSides", Float ) = 8
        _PolygonSize("PolygonSize", Range(0, 1) ) = 1
        _GapWidth ("GapWidth", Range(0, 1)) = 0.25
        _Level ("Level", Float ) = 8
        _Percentage ("Percentage", Range(0, 1)) = 1
        _CenterRadius ("CenterRadius", Range(0, 1)) = 0.25
        [MaterialToggle] _UseTimeLoop ("UseTimeLoop", Float ) = 1
        _LoopSpeed ("LoopSpeed", Float ) = 1
        _IconTex ("IconTex", 2D) = "white" {}
        _IconSize ("IconSize", Range(0, 1)) = 0
        _BG_Opacity ("BG_Opacity", Range(0, 1)) = 0.25
        [MaterialToggle] _UseCircle ("UseCircle", Float ) = 0
        
    }
    SubShader {
        Tags {
            "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent"
        }
        Pass {
           
            
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            
            float4 _ColorOutside;
            float _Level;
            float _PolygonSides;
            float _PolygonSize;
            float _GapWidth;
            float _Percentage;
            float4 _ColorInside;
            float _Emission;
            float _CenterRadius;
            float _LoopSpeed;
            fixed _UseTimeLoop;
            sampler2D _IconTex; 
            float4 _IconTex_ST;
            float _IconSize;
            float _BG_Opacity;
            float _UseCircle;

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };
            v2f vert (appdata v) {
                v2f o = (v2f)0;
                o.uv = v.uv;
                o.pos = UnityObjectToClipPos( v.vertex );
                return o;
            }
            
            



            float DrawPolygon01 (float2 uv, float radius, float sides)
            {
              uv=uv*2-1;
              uv/=radius;
              float angle = atan2(uv.x,uv.y)+3.1418926;
              sides= 6.28318530718/sides;
              float shape=cos(floor(0.5+angle/sides)*sides-angle)*length(uv);
              return 1-shape;
            }

            void ShapeFlow02(float shape , float radius , float gapWidth, float level ,
                float usetime, out float flowPoly , out float centerRad, out float gapPoly){
                //中央半径
                centerRad = saturate((1 - shape - radius ) / (1 - radius));
                 //分割多边形
                gapPoly = step(gapWidth,frac(centerRad*level));
                //图形收缩动效               
                flowPoly =step(usetime*1.01,floor(centerRad * level) / (level - 1))*gapPoly;
            }

            float UseTimeLoop(float loopSpeed , float percent, float usetimeToggle){
                float loopTime = saturate((abs(frac(loopSpeed)*2-1)*1.5-0.5));
                float usetime = lerp( percent, loopTime, usetimeToggle );
                return usetime;
            }

             
 





            fixed4 frag (v2f i ) : SV_Target {

                float2 uv =i.uv;

                float polygon =DrawPolygon01(uv,_PolygonSize,_PolygonSides); 
                float circle = saturate(1-length(uv-0.5)*2);
                float useShape =lerp(polygon,circle,_UseCircle);//判断用那种形状

                //遮罩
                float mask =ceil(useShape);
               
                //循环时间
                                   
                float usetime = UseTimeLoop(_Time.y*_LoopSpeed, _Percentage,_UseTimeLoop );
                
                float flowPoly;
                float centerRad;
                float gapPoly;
                //图形收缩动效               
                ShapeFlow02(useShape,_CenterRadius,_GapWidth,_Level,usetime,flowPoly,centerRad,gapPoly);
                
                //图标uv居中
                float2 iconUV = uv*2-1;
                iconUV = (iconUV+_IconSize)/(_IconSize*2);
                iconUV =TRANSFORM_TEX(iconUV, _IconTex);
                float iconColor = tex2D(_IconTex,iconUV).a;
                iconColor = iconColor*(1.0 - usetime);
                
                float3 emissive = lerp(_ColorInside.rgb,_ColorOutside.rgb,centerRad)*_Emission;
                float3 finalColor = emissive;
                

                return fixed4(finalColor,saturate(flowPoly*mask+gapPoly*_BG_Opacity+iconColor));
                
               

            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    
}
